﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace netWebFroms
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Show.Text = "";
        }
        //登录
        protected void Enter_Click(object sender, EventArgs e)
        {
            var uid = txtUsername.Text.Trim();
            var pwd = txtPassword.Text.Trim();
            var sql = string.Format("select * from Users where Username ='{0}'",uid);
            var dt = DbHelper.GetData(sql);

            if(dt.Rows .Count == 1)
            {
                var res = dt.Rows[0];
                var IsActived = (bool)res.ItemArray[3];
                var IsDeleted = (bool)res.ItemArray[4];
                var Password = res.ItemArray[2].ToString();

                if(IsDeleted ==true)
                {
                    Show.Text = "账户不存在";
                }
                else
                {
                    if(IsActived)
                    {
                        if (pwd == Password)
                        {
                            Response.Redirect("Default.aspx");
                        }
                        else
                        {
                            Show.Text = "账户密码输入错误，请重新输入！！！";
                        }
                    }
                    else
                    {
                        Show.Text = "账户已被删除或者冻结";
                    }
                }
            }
            else
            {
                Show.Text = "账户输入错误";
            }
        }

        //注册
        protected void Register_Click(object sender, EventArgs e)
        {
            var uid = txtUsername.Text.Trim();
            var pwd = txtPassword.Text.Trim();
            var sql = string.Format("select * from Users where Username ='{0}'", uid);
            var dt2 = DbHelper.GetData(sql);

            if(dt2.Rows.Count == 0)
            {
                var hhh = string.Format("insert into Users(Username,Password)values('{0}','{1}') ", uid, pwd);
                DbHelper.AddOrUpdateOrDate(hhh);
                Show.Text = "账户已注册完成,可以登录！！！";
            }
            else
            {
                Show.Text = "账户已被注册";
            }
        }
    }
}